#include "main.h"
#include "dmr_delay.h"
#include "FreeRTOS.h"
#include "task.h"

#include "dmr_gpio.h"
#include "dmr_log.h"
#include "dmr_bsp.h"


#define SM_CONSOLE_TASK_STACK_SIZE    (64)
static 	StaticTask_t 		sm_console_task_buffer;
static 	StackType_t 		sm_console_task_stack[SM_CONSOLE_TASK_STACK_SIZE] = {0};

void task1_task(void *pvParameters);

//任务优先级
#define TASK2_TASK_PRIO                3
//任务堆栈大小        
#define TASK2_STK_SIZE                 64  
//任务句柄
static 	StaticTask_t 		Task2Task_buffer;
static 	StackType_t 		Task2Task_stack[TASK2_STK_SIZE] = {0};
//任务函数
void task2_task(void *pvParameters);




static StackType_t IdleTaskStack[configMINIMAL_STACK_SIZE];
static StackType_t TimerTaskStack[configTIMER_TASK_STACK_DEPTH];
static StaticTask_t IdleTaskTCB;
static StaticTask_t TimerTaskTCB;



void vApplicationGetIdleTaskMemory(StaticTask_t **ppxIdleTaskTCBBuffer,StackType_t **ppxIdleTaskStackBuffer,uint32_t *pulIdleTaskStackSize)
{

*ppxIdleTaskTCBBuffer=&IdleTaskTCB;

*ppxIdleTaskStackBuffer=IdleTaskStack;

*pulIdleTaskStackSize=configMINIMAL_STACK_SIZE;

}



void vApplicationGetTimerTaskMemory(StaticTask_t **ppxTimerTaskTCBBuffer,StackType_t **ppxTimerTaskStackBuffer,uint32_t *pulTimerTaskStackSize)
{

*ppxTimerTaskTCBBuffer=&TimerTaskTCB;

*ppxTimerTaskStackBuffer=TimerTaskStack;

*pulTimerTaskStackSize=configTIMER_TASK_STACK_DEPTH;

}





dmr_gpioHandle rLed_Handle = {0};
dmr_gpioHandle gLed_Handle = {0};
/**
 * [url=home.php?mod=space&uid=247401]@brief[/url]  Main program.
 */
int main(void)
{ 
	
	dmr_bspInit();
	
	//PD13
	rLed_Handle.gpiox 	= GPIOD;
	rLed_Handle.pin		= GPIO_PIN_13;
	//推挽模式
	rLed_Handle.mode 	= GPIO_MODE_OUT_PP;
	//上拉
	rLed_Handle.pull 	= GPIO_PULL_UP;
	//低速
	rLed_Handle.speed 	= GPIO_SLEW_RATE_SLOW;
	//2ma驱动电流
	rLed_Handle.current = GPIO_DS_2MA;
	//初始IO电平
	rLed_Handle.initLevel = DMR_PIN_INIT_LEVEL_HIGE;
	//初始化GPIO配置
	dmr_gpioInit(&rLed_Handle);
	
	
	//LED_G句柄
	
	//PC13
	gLed_Handle.gpiox 	= GPIOC;
	gLed_Handle.pin		= GPIO_PIN_13;
	//推挽模式
	gLed_Handle.mode 	= GPIO_MODE_OUT_PP;
	//上拉
	gLed_Handle.pull 	= GPIO_PULL_UP;
	//低速
	gLed_Handle.speed 	= GPIO_SLEW_RATE_SLOW;
	//2ma驱动电流
	gLed_Handle.current = GPIO_DS_2MA;
	//初始IO电平
	gLed_Handle.initLevel = DMR_PIN_INIT_LEVEL_LOW;
	//初始化GPIO配置
	dmr_gpioInit(&gLed_Handle);
	
	
xTaskCreateStatic(
					(TaskFunction_t   ) task1_task,
					(const char*      ) "task1_task",     /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
					(uint32_t         ) SM_CONSOLE_TASK_STACK_SIZE,
					(void*            ) NULL,
					(UBaseType_t      ) tskIDLE_PRIORITY+1,
					(StackType_t*     ) sm_console_task_stack,
					(StaticTask_t*    ) &sm_console_task_buffer
					);
					
xTaskCreateStatic((TaskFunction_t )task2_task,     
                (const char*    )"task2_task",   
                (uint16_t       )TASK2_STK_SIZE,
                (void*          )NULL,
                (UBaseType_t    )TASK2_TASK_PRIO,
                (StackType_t*     ) Task2Task_stack,
					(StaticTask_t*    ) &Task2Task_buffer
					);; 
			  
  vTaskStartScheduler();          //开启任务调度
}


//task1任务函数
void task1_task(void *pvParameters)
{
        u8 task1_num=0;
        
        while(1)
        {
            task1_num++;        //任务执1行次数加1 注意task1_num1加到255的时候会清零！！
            dmr_gpioTogger(&gLed_Handle);

			vTaskDelay(1000);                           //延时1s，也就是1000个时钟节拍        
        }
}

//task2任务函数
void task2_task(void *pvParameters)
{
        u8 task2_num=0;
        
        while(1)
        {
            task2_num++;        //任务2执行次数加1 注意task1_num2加到255的时候会清零！！
			dmr_gpioTogger(&rLed_Handle);

			vTaskDelay(500);                           //延时1s，也就是1000个时钟节拍        
        }
}
/**
 * @}
 */

/**
 * @brief Assert failed function by user.
 * @param file The name of the call that failed.
 * @param line The source line number of the call that failed.
 */
#ifdef USE_FULL_ASSERT
void assert_failed(const uint8_t* expr, const uint8_t* file, uint32_t line)
{
    while (1)
    {
    }
}
#endif // USE_FULL_ASSERT












